from typing import List


class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        dp_max, dp_min = nums[0], nums[0]
        max_p = nums[0]
        for i in range(1, len(nums)):
            # 　注意 这里必须是同时更新
            dp_max, dp_min = max(nums[i], dp_max*nums[i], dp_min*nums[i]), min(nums[i], dp_max*nums[i], dp_min*nums[i])
            max_p = max(max_p, dp_max)
        return max_p


def func(nums: List[int]) -> int:
    max_result = -float('inf')
    i_max, i_min = 1, 1
    for i in range(len(nums)):
        if nums[i] < 0:
            i_max, i_min = i_min, i_max
        i_max = max(i_max * nums[i], nums[i])
        i_min = min(i_min * nums[i], nums[i])
        max_result = max(max_result, i_max)
    return max_result


if __name__ == '__main__':
    t = [2, 3, -2, 4]
    s = Solution()
    print(s.maxProduct(t))
    print(func(t))
